//
//  QuestionsView.m
//  XinKaiFa55like
//
//  Created by junseek on 2017/1/17.
//  Copyright © 2017年 55like lj. All rights reserved.
//

#import "QuestionsView.h"
#import "NetEngine.h"
#import "RHMethods.h"

@interface QuestionsView ()<UITextViewDelegate>{
    //
    UIView *view_userContact;
    
    UITextView *txtContent;
    UILabel *lblDate;
    UIButton *btnDate;
    
    
    UIButton *btnDelete;
    
}
@end

@implementation QuestionsView

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code
        self.frame=[UIScreen mainScreen].bounds;
        
        self.backgroundColor=RGBACOLOR(0, 0, 0, 0.6);//[UIColor clearColor];
        
//        
//        UIControl *closeC=[[UIControl alloc]initWithFrame:self.bounds];
//        [closeC addTarget:self action:@selector(closeButtonClicked) forControlEvents:UIControlEventTouchUpInside];
//        [self addSubview:closeC];
        
        float fh=170*kScreenWidth/320;
        
        view_userContact=[[UIView alloc]initWithFrame:CGRectMake(30, (kScreenHeight-fh)/2.0, kScreenWidth-60, fh)];
        [self addSubview:view_userContact];
        view_userContact.backgroundColor=[UIColor whiteColor];//RGBACOLOR(255, 255, 255, 1);//
        view_userContact.clipsToBounds=YES;
        view_userContact.layer.borderWidth=0.5;
        view_userContact.layer.borderColor =[rgbLineColor CGColor];
        view_userContact.layer.cornerRadius =4.0;
        
        UIView * viewBGT=[RHMethods setTextViewWithFrame:CGRectMake(10, 10, W(view_userContact)-20, H(view_userContact)-60) leftRightEdge:5 topBottomEdge:5 font:fontTitle color:rgbTitleColor placeholder:@"请输入问题" delegate:self];
        viewBGT.backgroundColor=rgbGray;//[UIColor whiteColor];
        [view_userContact addSubview:viewBGT];
        
        txtContent=(UITextView *)[viewBGT viewWithTag:200];
        
        float fw=( W(view_userContact))/2.0;
        
       
        
        
        UIButton *btnCancel=[RHMethods buttonWithFrame:CGRectMake(0, H(view_userContact)-40, fw, 40) title:@"取消" image:nil bgimage:nil];
//        btnCancel.titleLabel.font=Font(20);
        [btnCancel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btnCancel addTarget:self action:@selector(cancelButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        btnCancel.backgroundColor=RGBCOLOR(213, 0, 43);
        [view_userContact addSubview:btnCancel];
        
        UIButton *btnOK=[RHMethods buttonWithFrame:CGRectMake(XW(btnCancel), Y(btnCancel), fw, 40) title:@"提交" image:nil bgimage:nil];
        //        btnOK.titleLabel.font=Font(20);
        btnOK.backgroundColor=RGBCOLOR(213, 0, 43);
        [btnOK setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btnOK addTarget:self action:@selector(okButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        [view_userContact addSubview:btnOK];
        
        UIView *viewLine=[[UIView alloc] initWithFrame:CGRectMake(XW(btnCancel), Y(btnOK)+12, 1, 20)];
        viewLine.backgroundColor=[UIColor whiteColor];
        [view_userContact addSubview:viewLine];
        
    }
    return self;
}
-(void)okButtonClicked{
    NSMutableDictionary *dict=[[NSMutableDictionary alloc]initWithDictionary:self.memoDic];
    
    NSString *strR=txtContent.text;
    if (![strR notEmptyOrNull]) {
        [SVProgressHUD showImage:nil status:@"请输入问题"];
        return;
    }
    
    [dict setValue:[[Utility Share] userId] forKey:@"uid"];
    [dict setValue:[[Utility Share] userToken] forKey:@"token"];
    [dict setValue:strR forKey:@"content"];
    
    [NetEngine createPostAction:XKFV3homescomment withParams:dict onCompletion:^(id resData, BOOL isCache) {
        if ([[resData valueForJSONStrKey:@"status"] isEqualToString:@"200"]) {
            txtContent.text=@"";
            [SVProgressHUD showSuccessWithStatus:[resData valueForJSONStrKey:@"info"]];
            if ([self.delegate respondsToSelector:@selector(QuestionsViewViewSubmint:object:)]) {
                [self.delegate QuestionsViewViewSubmint:self object:dict];
            }
            [self hidden];
        }else{
            [SVProgressHUD showImage:nil status:[resData valueForJSONStrKey:@"info"]];
        }
    }];
    
}
-(void)cancelButtonClicked{
    [self hidden];
}
-(void)closeButtonClicked{
    [self hidden];
}



#pragma mark UITextViewDelegate
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    //    if ([text isEqualToString:@"\n"]) {
    //        [textView resignFirstResponder];
    //    }
    return YES;
}
-(void)textViewDidChange:(UITextView *)textView
{
    UILabel *la = (UILabel *)[textView.superview viewWithTag:100];
    if (textView.text.length) {
        la.hidden = YES;
    }else{
        la.hidden = NO;
    }
}

#pragma mark serlector
-(void)handleKeyboardDidShow:(NSNotification *)notification{
    
    NSDictionary *info = [notification userInfo];
    CGRect keyboardFrame;
    [[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;
    CGFloat distanceToMove = kbSize.height;
    NSLog(@"---->动态键盘高度:%f",distanceToMove);
    if (distanceToMove<20) {
        return;
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        view_userContact.frameY=kScreenHeight-distanceToMove-H(view_userContact)-10;
    } completion:^(BOOL finished) {
        
    }];
}
- (void)handleKeyboardWillHide:(NSNotification *)notification
{
    [UIView animateWithDuration:0.3 animations:^{
        view_userContact.frameY=(kScreenHeight-H(view_userContact))/2.0;
    } completion:^(BOOL finished) {
        
    }];
}//

- (void)show
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hidden) name:@"push_showLogin" object:nil];
    
    
    //注册监听键盘事件
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
//    [txtContent becomeFirstResponder];
    
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
    
    self.hidden = NO;
    self.alpha = 0.0f;
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha=1.0f;
    } completion:^(BOOL finished) {
        //        [UIView animateWithDuration:tempF>300?0.4:0.2 animations:^{
        //        }];
    }];
    
    
    
}

- (void)hidden
{
    [txtContent resignFirstResponder];
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha=0.0f;
    } completion:^(BOOL finished) {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }];
    
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
